#! /bin/bash

# mk-basefile, create basefiles for some distributions
#
# Thomas Lange, Uni Koeln, 2011-2017
# based on the Makefile implementation of Michael Goetze
#
# Usage example: mk-basefile -J STRETCH64
# This will create a STRETCH64.tar.xz basefile.

# Supported distributions (each i386/amd64):
# Debian GNU/Linux stretch/buster
# Ubuntu 14.04/16.04/18.04
# CentOS 5/6/7/8
# Fedora 27 30
# Scientific Linux Cern 5/6
# DEEPIN 15.5 (PANDA)
# DEEPIN 15.10 (LION)
#
# fai official base images http://fai-project.org/download/basefiles/
# You can also download base images from https://download.openvz.org/template/precreated/ made by other people and rename it.
# from docker: openSUSE 42
# https://github.com/openSUSE/docker-containers-build/blob/6252e7bc2a411b3c5cfd5ed3b74e5839f7f7615b/x86_64/Dockerfile
# from docker fedora 27
# https://github.com/fedora-cloud/docker-brew-fedora/blob/425644cb22f4f31db65671bb3df75de4ee5b9742/x86_64//Dockerfile
# TODO: all base images are made from docker registry.
# Packages you might want to install to use this command:
# debootstrap, rinse, xz-utils


# Define your local mirros here
# For the first stage, set the CentOS/SLC mirror in /etc/rinse/rinse.conf
MIRROR_DEBIAN=http://mirrors.aliyun.com/debian/
MIRROR_DEEPIN=https://mirrors.tuna.tsinghua.edu.cn/deepin/
MIRROR_UBUNTU=http://mirrors.aliyun.com/ubuntu/
MIRROR_CENTOS=http://mirrors.aliyun.com/
MIRROR_FEDORA=http://mirrors.aliyun.com/fedora/

EXCLUDE_SQUEEZE=isc-dhcp-client,isc-dhcp-common,info,tasksel,tasksel-data
EXCLUDE_WHEEZY=info,tasksel,tasksel-data
EXCLUDE_JESSIE=info,tasksel,tasksel-data
EXCLUDE_STRETCH=info,tasksel,tasksel-data
EXCLUDE_BUSTER=tasksel,tasksel-data
EXCLUDE_PANDA=info
EXCLUDE_LION=info
EXCLUDE_SID=tasksel,tasksel-data

EXCLUDE_TRUSTY=dhcp3-client,dhcp3-common,info
EXCLUDE_XENIAL=udhcpc,dibbler-client,info
EXCLUDE_BIONIC=udhcpc,dibbler-client,info

# here you can add packages, that are needed very early
INCLUDE_DEBIAN=apt-transport-https


setarch() {

    l32=
    if [ X$1 = Xi386 ]; then
        l32=linux32
    fi
}

check() {

    if [ `id -u` != 0 ]; then
        echo "You must be root to create chroots."
        exit 1
    fi
    mknod $xtmp/test-dev-null c 1 3
    if [ $? -eq 1 ]; then
        echo "Cannot create device files on $xtmp, aborting."
        echo "Perhaps this directory is mounted with option nodev."
        rm -rf $xtmp
        exit 1
    fi
    echo test > $xtmp/test-dev-null
    if [ $? -eq 1 ]; then
        echo "Cannot create device files on $xtmp, aborting."
        echo "Perhaps this directory is mounted with option nodev."
        rm -rf $xtmp
        exit 1
    fi
    rm -f $xtmp/test-dev-null
}


mkpost-centos() {

    # set local mirror for rinse post script
    [ -z "$MIRROR_CENTOS" ] && return
    cat <<EOM > $xtmp/post
#! /bin/sh
mkdir -p $xtmp/etc/yum.repos.d/orig
cp -p $xtmp/etc/yum.repos.d/*.repo $xtmp/etc/yum.repos.d/orig
perl -pi -e 's,mirrorlist=,#mirrorlist=,; s,#baseurl=http://mirror.centos.org,baseurl=$MIRROR_CENTOS,;' $xtmp/etc/yum.repos.d/CentOS-Base.repo
EOM
    chmod 555 $xtmp/post
}


mkpost-slc() {

    # set local mirror for rinse post script
    ver=$1
    [ -z "$MIRROR_SLC" ] && return
    cat <<EOM > $xtmp/post
#! /bin/sh
mkdir -p $xtmp/etc/yum.repos.d/orig
cp -p $xtmp/etc/yum.repos.d/*.repo $xtmp/etc/yum.repos.d/orig
perl -pi -e 's,baseurl=http://linuxsoft.cern.ch,baseurl=$MIRROR_SLC,;' $xtmp/etc/yum.repos.d/slc$ver-os.repo
perl -pi -e 's,baseurl=http://linuxsoft.cern.ch,baseurl=$MIRROR_SLC,;' $xtmp/etc/yum.repos.d/slc$ver-updates.repo

EOM
    chmod 555 $xtmp/post
}


mkpost-fedora() {

    # set local mirror for rinse post script
    [ -z "$MIRROR_FEDORA" ] && return
    cat <<EOM > $xtmp/post
#! /bin/sh
mkdir -p $xtmp/etc/yum.repos.d/orig
cp -p $xtmp/etc/yum.repos.d/*.repo $xtmp/etc/yum.repos.d/orig
perl -pi -e 's,mirrorlist=,#mirrorlist=,; s,#baseurl=http://download.fedoraproject.org/pub/fedora/linux/,baseurl=$MIRROR_FEDORA,;' $xtmp/etc/yum.repos.d/fedora.repo
perl -pi -e 's,mirrorlist=,#mirrorlist=,; s,#baseurl=http://download.fedoraproject.org/pub/fedora/linux/,baseurl=$MIRROR_FEDORA,;' $xtmp/etc/yum.repos.d/fedora-updates.repo

EOM
    chmod 555 $xtmp/post
}

cleanup-deb() {

    chroot $xtmp apt-get clean
    rm -f $xtmp/etc/hostname $xtmp/etc/resolv.conf \
          $xtmp/var/lib/apt/lists/*_* $xtmp/usr/bin/qemu-*-static \
          $xtmp/etc/udev/rules.d/70-persistent-net.rules
    > $xtmp/etc/machine-id
}


cleanup-rinse() {

    # check if chroot works
    echo "Installed packages in chroot:"
    chroot $xtmp rpm -qa|sort
    echo -n "CHROOT rpm -qa: "
    chroot $xtmp rpm -qa|wc -l

    rm -f $xtmp/etc/resolv.conf $xtmp/post
    if [ -d $xtmp/etc/yum.repos.d/orig ]; then
        mv $xtmp/etc/yum.repos.d/orig/* $xtmp/etc/yum.repos.d/
        rm -rf $xtmp/etc/yum.repos.d/orig
    fi
}


tarit() {

    tar $attributes --one-file-system -C $xtmp -cf - . | $zip > $target.$ext
}


centos() {

    local arch=$1
    local vers=$2
    local domain=$(domainname)

    check
    setarch $arch
    mkpost-centos
    $l32 rinse --directory $xtmp --distribution centos-$vers --arch $arch --before-post-install $xtmp/post
    domainname $domain # workaround for #613377
    cleanup-rinse
    tarit
}


slc() {

    local arch=$1
    local vers=$2

    check
    setarch $arch
    mkpost-slc $vers
    $l32 rinse --directory $xtmp --distribution slc-$vers --arch $arch --before-post-install $xtmp/post
    cleanup-rinse
    tarit
}


fedora() {

    local arch=$1
    local vers=$2

    check
    setarch $arch
    mkpost-fedora
    $l32 rinse --directory $xtmp --distribution fedora-$vers --arch $arch --before-post-install $xtmp/post
    cleanup-rinse
    tarit
}


debgeneric() {

    local DIST=$1
    shift
    local mirror=$1
    shift
    local arch=$1

    dist=${DIST%%[0-9][0-9]}
    local exc="EXCLUDE_$dist"
    dist=${dist,,}

    check
    if [ -n "$INCLUDE_DEBIAN" ]; then
	local inc="--include=$INCLUDE_DEBIAN"
    fi

    if [ -n "$arch" ]; then
	qemu-debootstrap --arch $arch --exclude=${!exc} $inc $dist $xtmp $mirror
	target="${target}_${arch^^}"
    else
	if [[ $DIST =~ 64 ]]; then
            arch=amd64
	else
            arch=i386
	fi
	debootstrap --arch $arch --exclude=${!exc} $inc $dist $xtmp $mirror
    fi
    cleanup-deb
    tarit
}

prtdists() {

    echo "Available:

    CENTOS5_32   CENTOS5_64
    CENTOS6_32   CENTOS6_64
    CENTOS7_32   CENTOS7_64
                 CENTOS8_64
    SLC5_32      SLC5_64
    SLC6_32      SLC6_64
                 SLC7_64
                 FEDORA30_64
    TRUSTY32     TRUSTY64
    XENIAL32     XENIAL64
                 BIONIC64
    SQUEEZE32    SQUEEZE64
    WHEEZY32     WHEEZY64
    JESSIE32     JESSIE64
    STRETCH32    STRETCH64
    BUSTER32     BUSTER64
                 PANDA64
                 LION64
    SID32        SID64
"
}

usage() {

    cat <<EOF
mk-basefile, create minimal base files for a Linux distritubtion

   Copyright (C) 2011-2017 by Thomas Lange

Usage: mk-basefile [OPTION] ... DISTRIBUTION

   -s                   Show list of supported linux distributions
   -f ARCH              Build for foreign architecture ARCH.
   -d DIR               Use DIR for creating the temporary subtree structure.
   -z                   Use gzip for compressing the tar file.
   -J                   Use xz for compressing the tar file.
   -k                   Keep the temporary subtree structure, do not remove it.
   -h                   Print help.

 Usage example: mk-basefile -J STRETCH64
 This will create a STRETCH64.tar.xz basefile.

EOF
    exit 0
}

# main routine

ext=tar
zip=cat
attributes=
cleanup=1
attributes="--xattrs --selinux --acls"

while getopts ashzJd:kf: opt ; do
    case "$opt" in
        a) echo "$0: Warning. -a is ignored, because xtattrs, acls and selinux are always added." ;;
        d) export TMPDIR=$OPTARG ;;
        f) export ARCH=$OPTARG ;;
        z) zip="gzip -9"; ext=tar.gz ;;
        J) zip="xz -8" ext=tar.xz ;;
        k) cleanup=0 ;;
        h) usage ;;
        s) prtdists ; exit 0;;
        ?) exit 3 ;; # error in option parsing
    esac
done
shift $(($OPTIND - 1))

xtmp=$(mktemp --tmpdir -d basefiles.XXXXXXXX)
if [ $? -eq 1 ]; then
    echo "mktemp failed. Aborting."
    exit 2
fi
chmod 755 $xtmp

target=$1 # also the name of the output file

[ -z "$target" ] && usage
case "$target" in
    CENTOS5_32) centos i386 5 ;;
    CENTOS5_64) centos amd64 5 ;;
    CENTOS6_32) centos i386 6 ;;
    CENTOS6_64) centos amd64 6 ;;
    CENTOS7_32) centos i386 7 ;;
    CENTOS7_64) centos amd64 7 ;;
    CENTOS8_64) centos amd64 8 ;;
    SLC5_32) slc i386 5 ;;
    SLC5_64) slc amd64 5 ;;
    SLC6_32) slc i386 6 ;;
    SLC6_64) slc amd64 6 ;;
    SLC7_64) slc amd64 7 ;;
    FEDORA30_64) fedora amd64 30 ;;
    TRUSTY*|XENIAL*|BIONIC*)
        debgeneric $target $MIRROR_UBUNTU ;;
    SQUEEZE*|WHEEZY*|JESSIE*|STRETCH*|BUSTER*|SID*)
        debgeneric $target $MIRROR_DEBIAN $ARCH;;
    PANDA*)
        debgeneric $target $MIRROR_DEEPIN ;;
    LION*)
        debgeneric $target $MIRROR_DEEPIN ;;
    *) echo "Unknown distribution. Aborting."
       prtdists
       exit 99 ;;
esac

# cleanup
if [ $cleanup -eq 1 ]; then
   rm -rf $xtmp
fi
